<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends MY_Model {
    
    protected $TB_USER = 'dc_user';
    protected $USER_COLS = array(
        'user_id',
        'user_wb_uid',
        'user_wb_screen_name'
    );
    
    public function __construct() {
        parent::__construct();
    }
    
    public function add_user($wb_uid, $wb_screen_name=null) {
        $query = $this->db->query("INSERT INTO $this->TB_USER 
            (`user_wb_uid`, `user_wb_screen_name`) VALUES 
            ('$wb_uid', '$wb_screen_name')");
        return $query ? $this->db->insert_id() : false;
    }
    
    public function get_user($id) {
        $user_cols = implode(',', $this->USER_COLS);
        $query = $this->db->query("SELECT $user_cols FROM $this->TB_USER 
            WHERE `user_id` = '$id' LIMIT 1");
        return $query->row_array();
    }
    
    public function get_user_by_wb_uid($wb_uid) {
        $user_cols = join(', ', $this->USER_COLS);
        $query = $this->db->query("SELECT $user_cols FROM $this->TB_USER
            WHERE `user_wb_uid` = '$wb_uid' LIMIT 1");
        return $query->row_array();
    }
    
    public function get_user_id_by_wb_uid($wb_uid) {
        $query = $this->db->query("SELECT `user_id` FROM $this->TB_USER
            WHERE `user_wb_uid` = '$wb_uid' LIMIT 1");
        return $query->num_rows() ? current($query->row()) : false;
    }
    
}